﻿using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Collections.Generic;

public class LocalizedStringsGenerator 
{
	public void Init()
	{
		//System.Diagnostics.Debugger.Launch();

		const string stringsFileName = "strings.json";
		var content = File.ReadAllText(stringsFileName);

		var file = File.Create("LocalizedStrings_Items.cs");

		var items = new StringBuilder();
		var resetCache = new StringBuilder();

		var dict = new Dictionary<string, string>();

		using (var doc = JsonDocument.Parse(content))
		{
			foreach (var property in doc.RootElement.EnumerateObject())
			{
				if (dict.ContainsKey(property.Name))
					throw new InvalidOperationException(property.Name);

				dict.Add(property.Name, property.Value.GetString());
			}
		}

		foreach (var p in dict)
		{
			var prop = p.Key;
			var xmlComment = p.Value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");

			items.AppendLine($@"	/// <summary>
	/// {xmlComment}
	/// </summary>
	public const string {prop}Key = nameof({prop});

	private static string _{prop};

	/// <summary>
	/// {xmlComment}
	/// </summary>
	public static string {prop} => _{prop} ??= GetString({prop}Key);").AppendLine();

			resetCache.AppendLine($"\t\t_{prop} = null;");
		}

		var str = $@"// <auto-generated />

namespace StockSharp.Localization;

partial class LocalizedStrings
{{
	private const string _stringsFileName = ""{stringsFileName}"";

{items}

	/// <summary>
	/// Reset cached fields state.
	/// </summary>
	public static void ResetCache()
	{{
{resetCache}
	}}
}}";

		var byteContents = System.Text.Encoding.Default.GetBytes(str);
		file.Write(byteContents, 0, byteContents.Length);
		file.Flush();
		file.Close();

	}
}
